package com.lib.controller.answer;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lib.model.dto.AnswerDto;
import com.lib.paging.PageBean;
import com.lib.paging.ResultLists;

@Controller
public class InformationBoard {
	
	@Autowired
	private com.lib.model.answer.IBoardService service;//qna 
	

	
	@RequestMapping("/answerlist.do")
	public ModelAndView answer(HttpServletRequest request,HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView();
		
		String sstartPage= request.getParameter("startPage");
		String sstartBlock= request.getParameter("startBlock");
		String searchType =request.getParameter("searchType");
		String searchValue= request.getParameter("searchValue");
		
		System.out.println("1----"+searchType);
		System.out.println("2----"+searchValue);
		
		int startPage = 1;
		int startBlock = 1;
		
		if (sstartPage != null) {
			startPage = Integer.parseInt(sstartPage);
		}
		if (sstartBlock != null) {
			startBlock = Integer.parseInt(sstartBlock);
		}
		

		ResultLists<AnswerDto> answerlist = 
			service.getBoardList(startPage, startBlock,searchType,searchValue);

		mav.addObject("lists", answerlist);
		mav.setViewName("boardlist");
		
		return mav;
	}
	
	
	
	@RequestMapping("/writeboardsave.do")
	public ModelAndView boardwrite(HttpServletRequest request, HttpServletResponse response,AnswerDto dto)
	{
		ModelAndView mav = new ModelAndView();
		
		boolean isS = service.writeBoard(dto);
		if(isS){
			mav.setViewName("redirect:/answerlist.do");
		}
		return mav;
	}
	@RequestMapping("/boarddetail.do")
	public ModelAndView boardDetail(HttpServletRequest request , HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView();
		int questionboard_num = Integer.parseInt(request.getParameter("questionboard_num"));
		
		AnswerDto bdto = service.getBoardDetail(questionboard_num);
		request.setAttribute("detail", bdto);
		mav.setViewName("detail");
		
		return mav;
	}
	
	@RequestMapping("/boardupdate.do")
	public ModelAndView bfboardupdate(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView();
		
		int questionboard_num = Integer.parseInt(request.getParameter("questionboard_num"));
		AnswerDto bdto = service.getBoardDetail(questionboard_num);
		
		request.setAttribute("detail", bdto);
		mav.setViewName("bfupdate");	
		return mav;
	}
	
	@RequestMapping("/boardupdate2.do")
	public ModelAndView boardupdate(HttpServletRequest request , HttpServletResponse response, AnswerDto dto)
	{
		ModelAndView mav = new ModelAndView();
		
		boolean isS = service.updateBoardDetail(dto);
		
		mav.setViewName("redirect:/boarddetail.do?questionboard_num="+dto.getQuestionboard_num());
		return mav;
	}
	@RequestMapping("/delete.do")
	public ModelAndView delete(HttpServletRequest request , HttpServletResponse response){
		ModelAndView mav = new ModelAndView();
		
		int questionboard_num = Integer.parseInt(request.getParameter("questionboard_num"));
		
		boolean isS = service.deleteBoard(questionboard_num);
		
		mav.setViewName("redirect:/answerlist.do");
		
		return mav;
	}
	
	@RequestMapping("/ansboard.do")
	public ModelAndView bfansboard(HttpServletRequest request , HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView();
		int questionboard_num = Integer.parseInt(request.getParameter("questionboard_num"));
		AnswerDto bdto = service.getBoardDetail(questionboard_num);
		request.setAttribute("detail", bdto);
		mav.setViewName("replyform");
		
		return mav;
	}
	@RequestMapping("/ansboardsave.do")
	public ModelAndView bfansboard(HttpServletRequest request , HttpServletResponse response,AnswerDto dto)
	{
		ModelAndView mav = new ModelAndView();
		int questionboard_num = Integer.parseInt(request.getParameter("questionboard_num"));
		dto.setQuestionboard_num(questionboard_num);
		//System.out.println(questionboard_num);
		boolean isS = service.addReply(dto);
		System.out.println(dto);
		if(isS){
			mav.setViewName("redirect:/answerlist.do");
		}
		
		return mav;
	}
	@RequestMapping("/writeboard.do")
	public String boardwriteform(HttpServletRequest request , HttpServletResponse response)
	{
		return "writeboard";
	}
}
